package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryAsphaltLowTemperatureElongationBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryAsphaltLowTemperatureElongationVo;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltLowTemperatureElongation;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltLowTemperatureElongationMapper;
import com.ruoyi.zhgd.service.INLaboratoryAsphaltLowTemperatureElongationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青低温延伸度试验仪Service业务层处理
 *
 * @author cj
 * @date 2023-09-20
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryAsphaltLowTemperatureElongationServiceImpl implements INLaboratoryAsphaltLowTemperatureElongationService {

    private final NLaboratoryAsphaltLowTemperatureElongationMapper baseMapper;

    /**
     * 查询沥青低温延伸度试验仪
     */
    @Override
    public NLaboratoryAsphaltLowTemperatureElongationVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青低温延伸度试验仪列表
     */
    @Override
    public TableDataInfo<NLaboratoryAsphaltLowTemperatureElongationVo> queryPageList(NLaboratoryAsphaltLowTemperatureElongationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryAsphaltLowTemperatureElongation> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryAsphaltLowTemperatureElongationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青低温延伸度试验仪列表
     */
    @Override
    public List<NLaboratoryAsphaltLowTemperatureElongationVo> queryList(NLaboratoryAsphaltLowTemperatureElongationBo bo) {
        LambdaQueryWrapper<NLaboratoryAsphaltLowTemperatureElongation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryAsphaltLowTemperatureElongation> buildQueryWrapper(NLaboratoryAsphaltLowTemperatureElongationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryAsphaltLowTemperatureElongation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryAsphaltLowTemperatureElongation::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getResId()), NLaboratoryAsphaltLowTemperatureElongation::getResId, bo.getResId());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), NLaboratoryAsphaltLowTemperatureElongation::getFlag, bo.getFlag());
        lqw.eq(bo.getLength1() != null, NLaboratoryAsphaltLowTemperatureElongation::getLength1, bo.getLength1());
        lqw.eq(bo.getLength2() != null, NLaboratoryAsphaltLowTemperatureElongation::getLength2, bo.getLength2());
        lqw.eq(bo.getLength3() != null, NLaboratoryAsphaltLowTemperatureElongation::getLength3, bo.getLength3());
        lqw.eq(bo.getSpeed1() != null, NLaboratoryAsphaltLowTemperatureElongation::getSpeed1, bo.getSpeed1());
        lqw.eq(bo.getSpeed2() != null, NLaboratoryAsphaltLowTemperatureElongation::getSpeed2, bo.getSpeed2());
        lqw.eq(bo.getCurrTemp() != null, NLaboratoryAsphaltLowTemperatureElongation::getCurrTemp, bo.getCurrTemp());
        lqw.eq(bo.getSetTemp() != null, NLaboratoryAsphaltLowTemperatureElongation::getSetTemp, bo.getSetTemp());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginalData()), NLaboratoryAsphaltLowTemperatureElongation::getOriginalData, bo.getOriginalData());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleId()), NLaboratoryAsphaltLowTemperatureElongation::getSampleId, bo.getSampleId());
        return lqw;
    }

    /**
     * 新增沥青低温延伸度试验仪
     */
    @Override
    public Boolean insertByBo(NLaboratoryAsphaltLowTemperatureElongationBo bo) {
        NLaboratoryAsphaltLowTemperatureElongation add = BeanUtil.toBean(bo, NLaboratoryAsphaltLowTemperatureElongation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青低温延伸度试验仪
     */
    @Override
    public Boolean updateByBo(NLaboratoryAsphaltLowTemperatureElongationBo bo) {
        NLaboratoryAsphaltLowTemperatureElongation update = BeanUtil.toBean(bo, NLaboratoryAsphaltLowTemperatureElongation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryAsphaltLowTemperatureElongation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青低温延伸度试验仪
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils queryPage(Page page, String resId) {
        List<Map> list = baseMapper.queryPages(page, resId);
        return new PageUtils(page.setRecords(list));
    }
}
